package com.ybxx.asset.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.asset.model.Building;
import com.ybxx.asset.model.Company;
import com.ybxx.asset.model.vo.BuildingVO;
import com.ybxx.asset.service.BuildingService;
import com.ybxx.asset.service.CompanyService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.ExportExcel;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.hr.model.HrUser;
import com.ybxx.hr.model.vo.HrUserVO;

public class BuildingAction extends BaseAction {
	@Resource
	private CompanyService companyService;
	@Resource
	private BuildingService buildingService;
	private List<Company> companys;
	private List<Building> buildings;
	private List<BuildingVO> vos;
	private Building building;
	private String msg="succ";
	/*打印*/
	public String print(){
		buildings=buildingService.exportExcel_Print(building);
		vos=new ArrayList<BuildingVO>();
		if(buildings!=null){
			int i=1;
			for(Building building:buildings){
				BuildingVO vo=new BuildingVO();
				vo.setSeq(i+"");
				vo.setBname(building.getBname());
				vo.setBookvalue(building.getBookvalue());
				vo.setBuildarea(building.getBuildarea());
				vo.setBuildmonth(building.getBuildmonth());
				vo.setCname(building.getCompany().getCname());
				vo.setContent(building.getContent());
				vo.setLayer(building.getLayer());
				vo.setMdept(building.getMdept());
				vo.setNetbookvalue(building.getNetbookvalue());
				vo.setPrice(building.getPrice());
				vo.setQzbh(building.getQzbh());
				vo.setStruct(building.getStruct());
				vos.add(vo);
				i++;
			}
		}
		return "print";
	}
	/*导出*/
	public String exportExcel(){
		buildings=buildingService.exportExcel_Print(building);
		vos=new ArrayList<BuildingVO>();
		if(buildings!=null){
			int i=1;
			for(Building building:buildings){
				BuildingVO vo=new BuildingVO();
				vo.setSeq(i+"");
				vo.setBname(building.getBname());
				vo.setBookvalue(building.getBookvalue());
				vo.setBuildarea(building.getBuildarea());
				vo.setBuildmonth(building.getBuildmonth());
				vo.setCname(building.getCompany().getCname());
				vo.setContent(building.getContent());
				vo.setLayer(building.getLayer());
				vo.setMdept(building.getMdept());
				vo.setNetbookvalue(building.getNetbookvalue());
				vo.setPrice(building.getPrice());
				vo.setQzbh(building.getQzbh());
				vo.setStruct(building.getStruct());
				vos.add(vo);
				i++;
			}
		}
		ExportExcel<BuildingVO> ex=new ExportExcel<BuildingVO>();
		OutputStream out;
		try {
			out = super.getResponse().getOutputStream();
			super.getResponse().reset();// 清空输出流
			super.getResponse().setContentType("text/html; charset=UTF-8");
			super.getResponse().setHeader("Content-disposition",
					"attachment; filename="+DateUtil.formatDateYYYYMMdd(new Date())+".xls");// 设定输出文件头
			super.getResponse().setContentType("application/msexcel");// 定义输出类型
			String[] headers={"序号","企业名称","权证编号","建筑物名称","结构","层数","建成年月","建筑面积","成本单价","账面原值","账面净值","管理（使用）部门","备注"};
	        ex.exportExcel("房屋建筑物信息",headers, vos, out);
	        out.flush();
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/*删除*/
	public String remove(){
		try{
			buildingService.remove(building.getBid());
		}catch(Exception ex){
			msg="房屋建筑物删除失败";
		}finally{
			return "succ";
		}
	}
	/*加载查看时候需要展示的数据*/
	public String view(){
		if(building!=null && StringUtils.isNotEmpty(building.getBid())){
			building=buildingService.get(building.getBid());
		}
		return "view";
	}
	public String init(){
		companys=companyService.getAll();
		return "init";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		buildings=buildingService.queryPage(building,filter);
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	/*保存新增，修改信息*/
	public String save(){
		if(StringUtils.isEmpty(building.getBid())){
			building.setBid(UUIDGenerator.getUUID());
		}
		try{
			buildingService.save(building);
		}catch(Exception ex){
			msg="房屋建筑物信息保存失败.";
		}finally{
			return "succ";
		}
	}
	/*加载修改时候需要展示的数据*/
	public String load(){
		companys=companyService.getAll();
		if(building!=null && StringUtils.isNotEmpty(building.getBid())){
			building=buildingService.get(building.getBid());
		}
		return "load";
	}
	public List<Company> getCompanys() {
		return companys;
	}
	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}
	public List<Building> getBuildings() {
		return buildings;
	}
	public void setBuildings(List<Building> buildings) {
		this.buildings = buildings;
	}
	public Building getBuilding() {
		return building;
	}
	public void setBuilding(Building building) {
		this.building = building;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<BuildingVO> getVos() {
		return vos;
	}
	public void setVos(List<BuildingVO> vos) {
		this.vos = vos;
	}

}
